package com.swak.fx.support;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.swak.codec.SafeEncoder;
import com.swak.storage.LocalStorage;
import com.swak.utils.Lists;
import com.swak.utils.Maps;
import com.swak.utils.StringUtils;

import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Window;

/**
 * 文件选择
 * 
 * @author 618lf
 */
public class FileChoosers {

	private static FileChooser fileChooser;
	private static LocalStorage Storage = LocalStorage.New();
	private static String CacheFile = FileChoosers.class.getName();

	private static Map<String, List<ExtensionFilter>> fileFilters = Maps.newHashMap();

	static {

		// 文件的过滤器
		fileFilters.put("File",
				Lists.newArrayList(new ExtensionFilter("All Files", "*.*"), new ExtensionFilter("Text Files", "*.txt"),
						new ExtensionFilter("Text Files", "*.zip", "*.rar", "*.7z"),
						new ExtensionFilter("Office Files", "*.xls", "*.xlsx", "*.doc", "*.docx", "*.ppt", "*.pptx",
								"*.pdf"),
						new ExtensionFilter("Image Files", "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp"),
						new ExtensionFilter("Audio Files", "*.mp3", "*.wav", "*.wmv", "*.wma", "*.avi", "*.mp4",
								"*.rmvb", "*.flv", "*.mov", "*.m2v", "*.mkv")));

		// 图片的过滤器
		fileFilters.put("Image",
				Lists.newArrayList(new ExtensionFilter("Image Files", "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp")));
	}

	/**
	 * 打开图片选择器
	 * 
	 * @param ownerWindow
	 * @return
	 */
	public static List<File> openImageChooser(final Window ownerWindow) {
		return openFileChooser(ownerWindow, fileFilters.get("Image"));
	}

	/**
	 * 打开文件选择器
	 * 
	 * @param ownerWindow
	 * @return
	 */
	public static List<File> openFileChooser(final Window ownerWindow) {
		return openFileChooser(ownerWindow, fileFilters.get("File"));
	}

	private static List<File> openFileChooser(final Window ownerWindow, List<ExtensionFilter> filters) {

		// 创建文件选择器
		buildFileChooser();

		// 设置过滤器
		fileChooser.getExtensionFilters().clear();
		fileChooser.getExtensionFilters().addAll(filters);

		// 打开目录缓存此次选择的目录
		List<File> files = fileChooser.showOpenMultipleDialog(ownerWindow);

		// 缓存最近打开文件所在的文件夹
		writeCacheFile(files);

		// 返回选择的文件
		return files;
	}

	private static void buildFileChooser() {

		// build
		if (fileChooser == null) {
			synchronized (FileChoosers.class) {
				fileChooser = new FileChooser();
				fileChooser.setTitle("打开文件");
				fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"),
						new ExtensionFilter("Text Files", "*.txt"),
						new ExtensionFilter("Text Files", "*.zip", "*.rar", "*.7z"),
						new ExtensionFilter("Office Files", "*.xls", "*.xlsx", "*.doc", "*.docx", "*.ppt", "*.pptx",
								"*.pdf"),
						new ExtensionFilter("Image Files", "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp"),
						new ExtensionFilter("Audio Files", "*.mp3", "*.wav", "*.wmv", "*.wma", "*.avi", "*.mp4",
								"*.rmvb", "*.flv", "*.mov", "*.m2v", "*.mkv"));
			}
		}

		// 缓存上次打开的目录
		String preference = readCacheFile();
		if (StringUtils.isNotBlank(preference)) {
			File file = new File(preference);
			if (file.exists() && file.isDirectory()) {
				fileChooser.setInitialDirectory(file);
			} else if (file.exists() && file.isFile()) {
				fileChooser.setInitialDirectory(file.getParentFile());
			}
		}
	}

	private static String readCacheFile() {
		byte[] data = Storage.get(CacheFile);
		if (data != null) {
			return SafeEncoder.encode(data);
		}
		return null;
	}

	private static void writeCacheFile(List<File> files) {
		if (files != null && !files.isEmpty()) {
			Storage.put(CacheFile, SafeEncoder.encode(files.get(0).getAbsolutePath()));
		}
	}
}